package main

import (
	"fmt"
	"github.com/tebeka/selenium"
	"io/ioutil"
	"os"
	"os/signal"
	"syscall"
)

var (
	sigs chan os.Signal
)

func init() {
	sigs = make(chan os.Signal, 1)
	signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
}
func main() {
	const (
		// These paths will be different on your system.
		seleniumPath = "D:/gopath/src/base/vender/selenium-server-standalone-3.8.1.jar"
		geckoDriverPath = "D:/gopath/src/base/vender/geckodriver.exe"
		port            = 8080
	)
	opts := []selenium.ServiceOption{
		selenium.GeckoDriver(geckoDriverPath), // Specify the path to GeckoDriver in order to use Firefox.
		selenium.Output(ioutil.Discard),            // Output debug information to STDERR.
	}
	selenium.SetDebug(false)
	service, err := selenium.NewSeleniumService(seleniumPath, port, opts...)
	if err != nil {
		panic(err) // panic is used only as an example and is not otherwise recommended.
	}
	defer service.Stop()

	// Connect to the WebDriver instance running locally.
	caps := selenium.Capabilities{"browserName": "firefox"}
	wd, err := selenium.NewRemote(caps, fmt.Sprintf("http://localhost:%d/wd/hub", port))
	if err != nil {
		panic(err)
	}
	defer wd.Quit()

	// Navigate to the simple playground interface.
	if err := wd.Get("https://www.baidu.com/"); err != nil {
		panic(err)
	}
	wd.SetImplicitWaitTimeout(4)
	input,err:=wd.FindElement(selenium.ByCSSSelector,"#kw")
	if err!=nil{
		panic(err)
	}
	input.SendKeys("selenium")
	search,err:=wd.FindElement(selenium.ByCSSSelector,"#su")
	if err!=nil{
		panic(err)
	}
	search.Click()
	<-sigs
}
